package com.example.shiwuyouproject.ui.fragment.home.activity

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.webkit.WebChromeClient
import android.widget.TextView
import androidx.lifecycle.Observer
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseBean
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityCoursedetailsBinding
import com.example.shiwuyouproject.ui.fragment.home.bean.GetFavoriteStarBean
import com.example.shiwuyouproject.ui.fragment.home.bean.GetPostGetBean
import com.example.shiwuyouproject.ui.fragment.home.viewmodel.CourseDetailsViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.ui.pup.LogOutPopupView
import com.example.shiwuyouproject.utils.GlideUtils
import com.example.shiwuyouproject.utils.TimeUtils
import com.lxj.xpopup.XPopup
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils
import java.util.*


/**
 *课程详情
 */
class CourseDetailsActivity :
    BaseVmActivity<ActivityCoursedetailsBinding, CourseDetailsViewModel>() {
    override fun viewModelClass(): Class<CourseDetailsViewModel> =
        CourseDetailsViewModel::class.java

    override fun getViewBinding(): ActivityCoursedetailsBinding {
        return ActivityCoursedetailsBinding.inflate(layoutInflater)
    }

    private var post_id = ""
    private var org_id=""
    override fun initView() {
        post_id = intent.getStringExtra("post_id").toString()


    }

    override fun initData() {
        mViewModel.getPostGet(post_id)
    }

    override fun setListener() {
        mBinding.tvBack.setOnClickListener { finish() }
        mBinding.ivCollection.setOnClickListener { //收藏
            if (mBinding.ivCollection.isSelected) {
                YUtils.showLoading(this, "取消收藏中...")
            } else {
                YUtils.showLoading(this, "收藏中...")
            }
            mViewModel.getFavoriteStarDel(post_id, mBinding.ivCollection.isSelected)
        }
        mBinding.ivShear.setOnClickListener { //分享

        }
        mBinding.tvShenQin.setOnClickListener { //立即申请
            XPopup.Builder(this)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .asCustom(LogOutPopupView(this, object :
                    LogOutPopupView.onClickListener {
                    override fun onCenter() {
                        YUtils.showLoading(this@CourseDetailsActivity, "申请中...")
                        mViewModel.getApplyAdd(org_id,post_id)
                    }
                }, stTiShi = "确认申请", stContext = "一个人最多同时申请3名职位,请谨慎申请！", stMyCenter = "继续申请"))
                .show()
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun observe() {
        mViewModel.getPostGetStatus.observe(this, Observer<GetPostGetBean>{
            post_id = it.id
            org_id=it.org_id
            mBinding.ivCollection.isSelected = it.star == "1"
            mBinding.tvTitle.text = it.title
            mBinding.tvMoneyH.text = it?.fee_start_show + "~" + it?.fee_end_show + "元/小时"
            mBinding.tvWeekTime.text =
                it.getWorkDayTitle() + " " + TimeUtils.HourMM(it?.start_time) + "-" + TimeUtils.HourMM(
                    it?.end_time
                )
            mBinding.tvStuNum.text = it.stu_num + "人报名"
            if (it?.tags == null || it?.tags?.length == 0) {
                mBinding.llTags.visibility = View.GONE
            } else {
                mBinding.llTags.visibility = View.VISIBLE
                val tagsLeng = it?.tags?.split(",")
                for (index in 0..tagsLeng?.size!!) {
                    val myItemView = LayoutInflater.from(this).inflate(R.layout.item_tags, null)
                    if (index != 0) {
                        myItemView.setPadding(10, 0, 0, 0)
                    } else {
                        myItemView.setPadding(0, 0, 0, 0)
                    }
                    val tvTitle: TextView = myItemView.findViewById(R.id.tvTagTitle)
                    tvTitle.text = tagsLeng[index]
                    mBinding.llTags.addView(myItemView)
                }
            }
            GlideUtils.fangImgPortrait(this, mBinding.haderImg, it.avatar)
            mBinding.tvOrgName.text = it.org_name
            mBinding.tvAddress.text = it.show_address
            mBinding.tvDetail.loadDataWithBaseURL(null, it.detail, "text/html", "utf-8", null)
            mBinding.tvDetail.settings.javaScriptEnabled = true
            mBinding.tvDetail.webChromeClient = WebChromeClient()
        })
        mViewModel.getFavoriteStarDelStatus.observe(this,  Observer<GetFavoriteStarBean>{
            YUtils.hideLoading()
            if (it.status == 1) {
                if (mBinding.ivCollection.isSelected) {
                    mBinding.ivCollection.isSelected = false
                    ToastUtil.show("该课程取消收藏成功!")
                } else {
                    mBinding.ivCollection.isSelected = true
                    ToastUtil.show("该课程收藏成功!")
                }
            }
        })
        mViewModel.getApplyAddStatus.observe(this, Observer<BaseBean<Objects>>{
            YUtils.hideLoading()
            if(it.success()){
                ToastUtil.show(it.message())
                finish()
            }else{
                ToastUtil.show(it.message())
            }

        })
    }
}